package teachersocket2

import java.io.{ObjectInputStream, ObjectOutputStream}
import java.net.{ServerSocket, Socket}

object MyResourceManager {

  def handlerRegisterMsg(username:String,password:String): ResultMsg ={
    //对消息的处理
    println("username: "+username + "password: "+ password)
    ResultMsg(1,"注册成功！！")
  }

  def handlerHeartbeat(hostname:String): ResultMsg ={
    println(hostname + "当前的时间："+System.currentTimeMillis())
    ResultMsg(1,"心跳成功成功！！")
  }



  def main(args: Array[String]): Unit = {
    val serverSocket: ServerSocket = new ServerSocket(8888)
    val clientSocket: Socket = serverSocket.accept()

    val inputStream = new ObjectInputStream(clientSocket.getInputStream)

    val outputStream = new ObjectOutputStream(clientSocket.getOutputStream)

    println(",,,,,,,,,,,,,,,,,,,,,,,")

    while(true){
      val clientMsg = inputStream.readObject()
      val result = clientMsg match {
        case RegisterMsg(username, password) => {
          //处理注册消息
          handlerRegisterMsg(username, password)
        }

        //
        case HeatBeart(hostname)  => {
          handlerHeartbeat(hostname);
        }
      }
      //服务端要向客户端发送 结果消息
      outputStream.writeObject(result)
      outputStream.flush()

    }


  }

}
